//
//  HHTabBar.m
//  百思不得姐
//
//  Created by hjl on 16/4/11.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "HHTabBar.h"

@implementation HHTabBar

// 设置字体
+ (void)initialize{
    
    UITabBarItem *item = [UITabBarItem appearance];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    [item setTitleTextAttributes:dict forState:UIControlStateNormal];
    
    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    [item setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
}

// 创建发布按钮
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // 设置背景色
        self.backgroundImage = [UIImage imageNamed:@"tabbar-light"];
        
        // 创建发布按钮
        UIButton *publishBtn = [[UIButton alloc]init];
        
        [publishBtn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        
        [publishBtn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        
        [publishBtn addTarget:self action:@selector(publishBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:publishBtn];
    }
    return self;
}

// 布局tabBar的子控件
- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat childX = 0;
    CGFloat childY = 0;
    CGFloat childH = self.height;
    CGFloat childW = HHWidth / 5;
    NSInteger index = 0;
    
    for (UIView *child in self.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            if ([child isKindOfClass:[UIButton class]]) {
                childX = 2 * childW;
                child.frame = CGRectMake(childX, childY + 2, childW, childH);
            }
            else{
                if (index == 2) {
                    index ++;
                }
                childX = index * childW;
                child.frame = CGRectMake(childX, childY, childW, childH);
                index ++;
            }
        }
    }
}

// 发布按钮的事件
- (void)publishBtnClick{
    
}

@end
